package com.xyht.sca_s.student_manage_system.modules.employmentApplication.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ExceptionCast;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.employmentApplication.entity.SmsEmploymentApplication;
import com.xyht.sca_s.student_manage_system.modules.employmentApplication.entity.SmsEmploymentApplicationDetail;
import com.xyht.sca_s.student_manage_system.modules.employmentApplication.entity.SmsEmploymentApplicationProcess;
import com.xyht.sca_s.student_manage_system.modules.employmentApplication.entity.vo.SmsEmploymentApplicationAllowVO;
import com.xyht.sca_s.student_manage_system.modules.employmentApplication.entity.vo.SmsEmploymentApplicationCancelVO;
import com.xyht.sca_s.student_manage_system.modules.employmentApplication.entity.vo.SmsEmploymentApplicationDetailVO;
import com.xyht.sca_s.student_manage_system.modules.employmentApplication.entity.vo.SmsEmploymentApplicationVO;
import com.xyht.sca_s.student_manage_system.modules.employmentApplication.mapper.SmsEmploymentApplicationDetailMapper;
import com.xyht.sca_s.student_manage_system.modules.employmentApplication.mapper.SmsEmploymentApplicationMapper;
import com.xyht.sca_s.student_manage_system.modules.employmentApplication.mapper.SmsEmploymentApplicationProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.employmentApplication.service.SmsEmploymentApplicationService;
import com.xyht.sca_s.student_manage_system.modules.newTips.util.NewTipsUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.employmentApplication.constant.employmentStatusConstant.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-05-23
 */
@Service
public class SmsEmploymentApplicationServiceImpl extends ServiceImpl<SmsEmploymentApplicationMapper, SmsEmploymentApplication> implements SmsEmploymentApplicationService {
    @Autowired
    SmsEmploymentApplicationMapper smsEmploymentApplicationMapper;
    @Autowired
    SmsEmploymentApplicationProcessMapper smsEmploymentApplicationProcessMapper;
    @Autowired
    SmsEmploymentApplicationDetailMapper smsEmploymentApplicationDetailMapper;
    @Autowired
    NewTipsUtil newTipsUtil;
    @Override
    public ResponseResult smsEmploymentApplication(SmsEmploymentApplicationVO smsEmploymentApplicationVO, String user_id) {
        List<SmsEmploymentApplicationProcess> fillProcessList =
                smsEmploymentApplicationProcessMapper.selectList(new QueryWrapper<SmsEmploymentApplicationProcess>()
                        .lambda()
                        .eq(SmsEmploymentApplicationProcess::getProcessIsApplicationEdit, true)
                        .eq(SmsEmploymentApplicationProcess::getProcessIsUse, true)
                        .orderByAsc(SmsEmploymentApplicationProcess::getProcessSort));
        if (fillProcessList != null && fillProcessList.size() > 0) {
            if (smsEmploymentApplicationVO.getProcessList() == null || smsEmploymentApplicationVO.getProcessList().size() < 1) {
                return CommonResult.failed(CommonCodeEnum.EMPLOYMENT_ALLOW_USER_NOT_SETTING);
            }
            //判断申请人是否将全部需要编辑流程填写
            List<SmsEmploymentApplicationProcess> filterList = fillProcessList
                    .stream()
                    .filter(process -> {
                                List<String> result = smsEmploymentApplicationVO.getProcessList()
                                        .stream()
                                        .filter(fillProcess -> fillProcess.getProcessId().equals(process.getId()))
                                        .map(SmsEmploymentApplicationDetailVO::getId)
                                        .collect(Collectors.toList());
                                //匹配的去除  未匹配的留下
                                if (result.size() > 0) {
                                    return false;
                                } else {
                                    return true;
                                }
                            }
                    )
                    .collect(Collectors.toList());
            if (filterList.size() > 0) {
                return CommonResult.failed(CommonCodeEnum.EMPLOYMENT_ALLOW_PROCESS_NOT_FILL);
            }
        }

        //创建申请表
        SmsEmploymentApplication addEmployment = new SmsEmploymentApplication();
        BeanUtils.copyProperties(smsEmploymentApplicationVO, addEmployment);
        addEmployment.setApplicationApplyUserId(user_id);
        addEmployment.setAllowStatus(EMPLOYMENT_STATUS_WAIT_ALLOW);
        smsEmploymentApplicationMapper.insert(addEmployment);

        if (addEmployment.getId() == null) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.FAIL));
        }
        String application_id = addEmployment.getId();

        //创建审批流程
        List<SmsEmploymentApplicationProcess> originProcessList =
                smsEmploymentApplicationProcessMapper.selectList(new QueryWrapper<SmsEmploymentApplicationProcess>()
                        .lambda()
                        .eq(SmsEmploymentApplicationProcess::getProcessIsUse, true)
                        .orderByAsc(SmsEmploymentApplicationProcess::getProcessSort));
        if (originProcessList == null || originProcessList.size() < 1) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.EMPLOYMENT_PROCESS_NOT_SETTING));
        }
        int sort = 1;
        for (SmsEmploymentApplicationProcess process : originProcessList) {
            //判断管理员是否配置初始流程
            if (!process.getProcessIsApplicationEdit()) {
                if (isNullOrEmpty(process.getProcessObjectId()) || isNullOrEmpty(process.getProcessObjectType())) {
                    ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.EMPLOYMENT_PROCESS_NOT_SETTING));
                }
            }

            SmsEmploymentApplicationDetail addDetail =
                    new SmsEmploymentApplicationDetail();

            addDetail.setApplicationId(application_id);
            addDetail.setProcessId(process.getId());
            addDetail.setProcessIsSign(process.getProcessIsSign());
            addDetail.setProcessIsStamp(process.getProcessIsStamp());
            addDetail.setProcessName(process.getProcessName());
            addDetail.setDetailAllowStatus(EMPLOYMENT_STATUS_WAIT_ALLOW);
            addDetail.setDetailSort(sort++);

            //申请人可编辑流程
            if (process.getProcessIsApplicationEdit()) {
                List<SmsEmploymentApplicationDetailVO> filterList = smsEmploymentApplicationVO.getProcessList()
                        .stream()
                        .filter(fillProcess -> fillProcess.getProcessId().equals(process.getId()))
                        .collect(Collectors.toList());
                if (filterList.size() < 1) {
                    ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.EMPLOYMENT_ALLOW_PROCESS_NOT_FILL));
                }
                SmsEmploymentApplicationDetailVO fillProcessReq = filterList.get(0);

                if (isNullOrEmpty(fillProcessReq.getProcessObjectId())) {
                    ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
                }
                addDetail.setProcessObjectId(fillProcessReq.getProcessObjectId());
                addDetail.setProcessObjectType(fillProcessReq.getProcessObjectType());
            } else {
                addDetail.setProcessObjectId(process.getProcessObjectId());
                addDetail.setProcessObjectType(process.getProcessObjectType());
            }
            smsEmploymentApplicationDetailMapper.insert(addDetail);
        }

        //消息通知
        //通知流程第一个节点
//        SmsEmploymentApplicationDetail firstDetail =
//                smsEmploymentApplicationDetailMapper.selectOne(new QueryWrapper<SmsEmploymentApplicationDetail>()
//                        .lambda()
//                        .eq(SmsEmploymentApplicationDetail::getApplicationId, application_id)
//                        .eq(SmsEmploymentApplicationDetail::getDetailSort, 1)
//                        .orderByAsc(SmsEmploymentApplicationDetail::getDetailSort));
//        RangeTypeEnum rangeTypeEnum;
//        if (firstDetail.getProcessObjectType() == EMPLOYMENT_OBJECT_TYPE_PERSON) {
//            rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_PERSON;
//        } else {
//            rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_ROLE;
//        }
//        List<String> rangeList = new ArrayList<>();
//        rangeList.add(firstDetail.getProcessObjectId());
//
//        newTipsUtil.addNewTips(TipsTypeEnum.TIPS_TYPE_ENUM_ALLOW,
//                user_id,
//                rangeTypeEnum,
//                rangeList,
//                AppTypeEnum.APP_TYPE_ENUM_PAYMENT_EXAMINE,
//                application_id,
//                firstDetail.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult smsEmploymentApplicationCancel(SmsEmploymentApplicationCancelVO cancelVO, String user_id) {
        if(isNullOrEmpty(cancelVO.getApplicationId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //查看审批表是否存在且为本人
        SmsEmploymentApplication smsEmploymentApplication
                = smsEmploymentApplicationMapper.selectOne(new QueryWrapper<SmsEmploymentApplication>()
        .lambda().eq(SmsEmploymentApplication::getId,cancelVO.getApplicationId())
        .eq(SmsEmploymentApplication::getApplicationApplyUserId,user_id));
        if(smsEmploymentApplication==null||isNullOrEmpty(smsEmploymentApplication.getId())){
            return CommonResult.failed(CommonCodeEnum.EMPLOYMENT_NOT_EXIST);
        }
        if(smsEmploymentApplication.getAllowStatus()!=EMPLOYMENT_STATUS_WAIT_ALLOW){
            return CommonResult.failed(CommonCodeEnum.EMPLOYMENT_CANCEL_FAIL_ALLOWED);
        }
        List<SmsEmploymentApplicationDetail> detailList =
                smsEmploymentApplicationDetailMapper.selectList(new QueryWrapper<SmsEmploymentApplicationDetail>()
                .lambda().eq(SmsEmploymentApplicationDetail::getApplicationId,cancelVO.getApplicationId())
                .eq(SmsEmploymentApplicationDetail::getDetailAllowStatus,EMPLOYMENT_STATUS_PASS));
        if(isNullOrEmpty(detailList)){
            return CommonResult.failed(CommonCodeEnum.EMPLOYMENT_CANCEL_FAIL_ALLOWED);
        }
        //撤销审批
        SmsEmploymentApplication smsEmploymentApplication1 = new SmsEmploymentApplication();
        smsEmploymentApplication1.setId(cancelVO.getApplicationId());
        smsEmploymentApplication1.setAllowStatus(EMPLOYMENT_STATUS_CANCEL);
        smsEmploymentApplication1.setEndTime(new Date());
        smsEmploymentApplicationMapper.updateById(smsEmploymentApplication1);
        //修改流程状态
        SmsEmploymentApplicationDetail updateEmploymentDetail = new SmsEmploymentApplicationDetail();
        updateEmploymentDetail.setDetailAllowStatus(EMPLOYMENT_STATUS_CANCEL);
        smsEmploymentApplicationDetailMapper.update(updateEmploymentDetail, new QueryWrapper<SmsEmploymentApplicationDetail>()
        .lambda()
        .eq(SmsEmploymentApplicationDetail::getApplicationId,cancelVO.getApplicationId()));
        //消息通知
        newTipsUtil.cancelFinishTips(cancelVO.getApplicationId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult smsEmploymentApplicationAllow(SmsEmploymentApplicationAllowVO allowVO, String user_id) {
        return null;
    }

    @Override
    public ResponseResult smsEmploymentApplicationList(Integer pageSize, Integer pageNum, Integer application_type, String user_id) {
        return null;
    }

    @Override
    public ResponseResult smsEmploymentApplicationDetail(String application_id, String user_id) {
        return null;
    }

    @Override
    public ResponseResult getSmsEmploymentApplicationOriginSettingProcess() {
        return null;
    }
}
